package com.wolf.gulimall.product.service.impl;

import com.wolf.gulimall.product.entity.AttrEntity;
import com.wolf.gulimall.product.service.AttrService;
import com.wolf.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.wolf.gulimall.product.vo.SkuItemVo;
import com.wolf.gulimall.product.vo.SpuItemAttrGroupVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wolf.common.utils.PageUtils;
import com.wolf.common.utils.Query;

import com.wolf.gulimall.product.dao.AttrGroupDao;
import com.wolf.gulimall.product.entity.AttrGroupEntity;
import com.wolf.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Slf4j
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 获取属性分组详情信息
     *
     * @param params    参数
     * @param catelogId 分组ID
     * @return 分组详情新信息
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        log.info("catelogId:{}", catelogId);
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and((obj) -> {
                obj.like("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        } else {
            queryWrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params), queryWrapper
            );
            return new PageUtils(page);
        }
    }

    /**
     * 根据分类ID查询所有的分组以及分组里面的属性信息
     *
     * @param catelogId 分类ID
     * @return 分组信息及属性信息
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupEntity> groupEntitles = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = groupEntitles.stream().map(groupEntitle -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(groupEntitle, attrGroupWithAttrsVo);
            List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroupWithAttrsVo.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(relationAttr);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return attrGroupWithAttrsVos;
    }

    /**
     * 根据商品spuId和三级分类Id 查询所有属性分组信息以及分组下所有属性对应的值
     *
     * @param spuId     商品spuId
     * @param catalogId 三级分类Id
     * @return 有属性分组信息以及分组下所有属性对应的值
     */
    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        return baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
    }


}